// pages/enter/enter.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    array: ['美国', '中国', '巴西', '日本'],
    index: 0,
    choose: true,
    name: '',
    mobile: '',
    brand: '',
    card1: '',
    card2: '',
    card3: '',
    card4: '',
    shopId: '',
    goodsId: ''
  },

  bindInp(e) {
    let value = e.detail.value
    let name = e.currentTarget.dataset.name
    this.setData({
      [`${name}`]: value
    })
  },

  choose(e) {
    let that = this
    let card = e.currentTarget.dataset.card
    wx.chooseImage({
      count: 1,
      success(res) {
        wx.showLoading()
        wx.$qiniu(res.tempFilePaths,
          res_ => {
            wx.hideLoading()
            that.setData({
              [`card${card}`]: res_[0]
            })
          })
      },
      fail(res) {
        console.log(res)
      }
    })
  },

  getInfos_() {
    wx.$https('shop.audit.detail', {
      type: this.data.type
    }).then(res => {
      this.setData({
        card1: res.cardImage1,
        card2: res.cardImage2,
        card3: res.headerImage,
        card4: res.image,
        name: res.realName,
        mobile: res.mobile,
        brand: res.name,
        shopId: res.shopId
      })
    })
  },

  getInfos() {
    wx.$https('goods.worker.audit.detail').then(res => {
      let array = this.data.array
      let index = ''
      for (let x in array) {
        if (array[x].categoryId == res.categoryId) index = x
      }
      this.setData({
        card1: res.cardImage1,
        card2: res.cardImage2,
        name: res.workName,
        mobile: res.workMobile,
        index: index,
        goodsId: res.goodsId
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let type = options.type
    let status = options.status
    this.setData({
      type: type
    })
    wx.setNavigationBarTitle({
      title: type == 1 ? '工人入驻' : (type == 2 ? '建材商入驻' : '辅材商入驻')
    })
    wx.$https('rule.find', {
      key: 'enterNotice'
    }).then(res => {
      this.setData({
        rule: res.value
      })
    })
    if (options.type == 1) {
      wx.$https('category.list', {
        type: 1
      }).then(res => {
        let array = []
        for (let x in res) {
          array = array.concat(res[x])
        }
        this.setData({
          array: array
        })
        if (status == 3) this.getInfos()
      })
    } else {
      if (status == 3) this.getInfos_(status)
    }
  },

  navRule() {
    wx.$nav('/pages/agreement/agreement', {
      url: encodeURIComponent(this.data.rule)
    })
  },

  chooseChange() {
    this.setData({
      choose: !this.data.choose
    })
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  confirm() {
    if (!this.data.choose) {
      wx.showToast({
        title: '请阅读并同意《入驻协议》',
        icon: 'none'
      })
      return
    }
    let that = this
    let type = that.data.type
    if (type == 1) {
      let args = {
        workName: that.data.name,
        workMobile: that.data.mobile,
        categoryId: that.data.array[that.data.index].categoryId,
        cardImage1: that.data.card1,
        cardImage2: that.data.card2
      }
      for (let x in args) {
        if (args[x] === '') {
          wx.showToast({
            title: '请填写完整',
            icon: 'none'
          })
          return
        }
      }
      wx.$https('goods.worker.add', {
        ...args,
        goodsId: this.data.goodsId
      }).then(res => {
        wx.showToast({
          title: '提交成功',
        })
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/my/my',
          })
        }, 1000)
      })
    } else {
      let args = {
        realName: that.data.name,
        mobile: that.data.mobile,
        name: that.data.brand,
        cardImage1: that.data.card1,
        cardImage2: that.data.card2,
        image: that.data.card3,
        headerImage: that.data.card4
      }
      for (let x in args) {
        if (args[x] === '') {
          wx.showToast({
            title: '请填写完整',
            icon: 'none'
          })
          return
        }
      }
      wx.$https('shop.add', {
        type: type,
        ...args,
        shopId: this.data.shopId
      }).then(res => {
        wx.showToast({
          title: '提交成功',
        })
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/my/my',
          })
        }, 1000)
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})